<template>
  <el-container>
    <el-container>
      <el-aside width="200px">
        <el-container>
          <el-main>
            <el-tree
              ref="tree"
              :data="deptData"
              :props="defaultProps"
              @node-click="handleNodeClick"
              style="padding: 20px"
            ></el-tree>
          </el-main>
        </el-container>
      </el-aside>
      <el-main>
        <el-card class="box-card search-form">
          <div class="list-search">
            <el-form ref="form" :model="form" :inline="true">
              <el-form-item label="昵称">
                <el-input v-model="search.nickname"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="onSubmit" size="small"
                  >查询</el-button
                >
              </el-form-item>
            </el-form>
            <el-button
              type="primary"
              @click="add"
              icon="el-icon-plus"
              size="small"
              >添加用户</el-button
            >
          </div>
        </el-card>
        <el-card class="box-card" style="margin-top: 10px">
          <el-table
            ref="table"
            :data="tableData"
            style="width: 100%"
            :row-style="{ height: '50px' }"
          >
            <el-table-column
              label="ID"
              prop="id"
              :show-overflow-tooltip="true"
              width="80"
            ></el-table-column>
            <el-table-column label="头像" width="80">
              <template #default="scope">
                <el-avatar :src="scope.row.avatar" size="small"></el-avatar>
              </template>
            </el-table-column>
            <el-table-column
              label="登录账号"
              prop="name"
              :show-overflow-tooltip="true"
            ></el-table-column>
            <el-table-column
              label="昵称"
              prop="nickname"
              :show-overflow-tooltip="true"
            ></el-table-column>
            <el-table-column label="状态" prop="status">
              <template #default="scope">
                <span v-if="scope.row.status == 1">
                  <el-tag size="medium" type="success">启用</el-tag>
                </span>
                <span v-else
                  ><el-tag size="medium" type="danger">禁用</el-tag></span
                >
              </template>
            </el-table-column>
            <el-table-column
              label="部门"
              prop="dept.name"
              :show-overflow-tooltip="true"
            ></el-table-column>
            <el-table-column
              label="角色"
              prop="role.name"
              :show-overflow-tooltip="true"
            ></el-table-column>
            <el-table-column
              label="添加时间"
              prop="create_time"
              :show-overflow-tooltip="true"
            ></el-table-column>
            <el-table-column label="操作" fixed="right" width="150">
              <template #default="scope">
                <el-button
                  type="text"
                  size="small"
                  @click="table_edit(scope.row, scope.$index)"
                  icon="el-icon-edit"
                  >编辑</el-button
                >
                <el-divider direction="vertical"></el-divider>
                <el-popconfirm
                  title="确定要删除吗？"
                  confirm-button-text="确定"
                  cancel-button-text="取消"
                  @confirm="table_del(scope.row, scope.$index)"
                >
                  <template #reference>
                    <el-button type="text" size="small" icon="el-icon-delete"
                      >删除</el-button
                    >
                  </template>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
          <div class="page">
            <el-pagination
              :hide-on-single-page="page.total < page.size ? true : false"
              background
              :current-page="page.currentPage"
              :page-sizes="page.pageSizes"
              :page-size="page.size"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            >
            </el-pagination>
          </div>
        </el-card>
      </el-main>
    </el-container>

    <save-dialog
      v-if="dialog.save"
      ref="saveDialog"
      @success="handleSaveSuccess"
      @closed="dialog.save = false"
    ></save-dialog>
  </el-container>
</template>

<script>
import saveDialog from "./save";

export default {
  name: "User",
  components: {
    saveDialog,
  },
  data() {
    return {
      dialog: {
        save: false,
      },
      form: {
        name: "",
      },
      page: {
        total: 0,
        currentPage: 1,
        size: 15,
        pageSizes: [15, 25, 35, 45],
      },
      search: {
        nickname: "",
        dept_id: 0,
        page: 1,
        pageSize: 15,
      },
      deptName: "",
      tableData: [],
      deptData: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
    };
  },
  mounted() {
    this.getDeptTree();
    this.getList();
  },
  methods: {
    table_edit(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("edit").setData(row);
      });
    },
    async table_del(row) {
      let res = await this.$api.user.del.get({ id: row.id });
      if (res.code == 0) {
        this.$message.success(res.msg);
        this.getList();
      } else {
        this.$message.error(res.msg);
      }
    },
    add() {
      if (!this.search.dept_id) {
        this.$message.error("请先点击选择部门");
        return false;
      }

      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("add").setData({
          dept_id: this.search.dept_id,
          status: 1,
          is_leader: 2,
          deptName: this.deptName,
        });
      });
    },
    onSubmit() {
      this.getList();
    },
    async getDeptTree() {
      let res = await this.$api.dept.list.get();
      if (res.code == 0) {
        let allNode = { id: 0, name: "所有" };
        res.data.unshift(allNode);
        this.deptData = res.data;
      } else {
        this.$message({
          message: res.msg,
          type: "error",
        });
      }
    },
    async getList() {
      let res = await this.$api.user.userList.get(this.search);
      this.page.total = res.data.total;
      this.tableData = res.data.rows;
    },
    handleNodeClick(data) {
      this.deptName = data.name;
      this.search.dept_id = data.id;
      this.getList();
    },
    // 分页规格改变
    handleSizeChange(val) {
      this.page.size = val;
      this.page.currentPage = 1;
      this.search.page = 1;
      this.search.limit = val;
      this.getList();
    },
    // 分页点击
    handleCurrentChange(val) {
      this.page.currentPage = val;
      this.search.page = val;
      this.search.limit = this.page.size;
      this.getList();
    },
    handleSaveSuccess() {
      this.getList();
    },
  },
};
</script>

<style>
.box-card {
  border: none !important;
  box-shadow: none !important;
  width: 100%;
}
.list-title {
  display: inline-block;
  color: #17233d;
  font-weight: 500;
  font-size: 20px;
  border-left: 5px solid #409eff;
  padding-left: 20px;
}
.el-form-item .el-form-item__label {
  font-weight: 400 !important;
  font-size: 12px !important;
}
.el-table td,
.el-table th {
  padding: 9px 0;
}
.page {
  margin-left: 20px;
  margin-top: 20px;
  display: flex;
  float: right;
  justify-content: space-between;
}
</style>
